﻿using System;
using System.Drawing;
using System.Net;
using System.Text.RegularExpressions;
using System.Web.UI.Design.WebControls;
using System.Windows.Forms;
using System.IO;
using System.Configuration;

namespace Argos.Windows.Forms
{
	public partial class RegularExpressionEditor: UserControl
	{
		public RegularExpressionEditor()
		{
			InitializeComponent();
			this.TestAreaTextbox.Text = Settings.Default.TestAreaContent;
		}

		public string SelectedPattern
		{
			get { return this.PatternTextbox.Text; }
			set { this.PatternTextbox.Text = value; }
		}



		private void BrowsePatternButton_Click(object sender, EventArgs e)
		{
			this.CommandsContextMenu.Show(this.BrowsePatternButton, new Point(0, this.BrowsePatternButton.Height));
		}

		private void PatternTextbox_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.Modifiers == Keys.None)
			{

			}
			else if (e.Modifiers == Keys.Alt)
			{
				if (e.KeyCode == Keys.Enter)
				{
					this.BrowsePatternButton.PerformClick();
					e.Handled = true;
					e.SuppressKeyPress = true;
				}
			}
			else if (e.Modifiers == Keys.Control)
			{
				if (e.KeyCode == Keys.Enter)
				{
					this.testToolStripMenuItem.PerformClick();
					e.Handled = true;
					e.SuppressKeyPress = true;
				}
			}
			else if (e.Modifiers == (Keys.Control | Keys.Shift))
			{
				if (e.KeyCode == Keys.Enter)
				{
					this.selectToolStripMenuItem.PerformClick();
					e.Handled = true;
					e.SuppressKeyPress = true;
				}
			}
		}

		private void selectToolStripMenuItem_Click(object sender, EventArgs e)
		{
			using (RegexEditorDialog dlg = new RegexEditorDialog(this.Site))
			{
				dlg.Text = "Regular Expression Selector";

				if (dlg.ShowDialog(this) == DialogResult.OK)
				{
					this.PatternTextbox.Text = dlg.RegularExpression;
					this.PatternTextbox.Focus();
				}
			}
		}

		private void testToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.TestAreaTextbox.Select(0, this.TestAreaTextbox.Text.Length);
			this.TestAreaTextbox.SelectionBackColor = this.TestAreaTextbox.BackColor;
			this.TestAreaTextbox.SelectionColor = this.TestAreaTextbox.ForeColor;

			if (string.IsNullOrEmpty(this.TestAreaTextbox.Text) || string.IsNullOrEmpty(this.PatternTextbox.Text))
			{
				return;
			}


			bool scrolled = false;
			MatchCollection matches = Regex.Matches(this.TestAreaTextbox.Text, this.PatternTextbox.Text);
			this.MatchCountLabel.Text = string.Format(this.MatchCountLabel.Tag.ToString(), matches.Count);

			foreach (Match match in matches)
			{
				if (match.Success)
				{
					this.TestAreaTextbox.Select(match.Index, match.Length);
					this.TestAreaTextbox.SelectionBackColor = SystemColors.Highlight;
					this.TestAreaTextbox.SelectionColor = SystemColors.HighlightText;

					if (!scrolled)
					{
						this.TestAreaTextbox.ScrollToCaret();
						scrolled = true;
					}
				}
			}
		}

		private void TestAreaTextbox_TextChanged(object sender, EventArgs e)
		{
			Settings.Default.TestAreaContent = this.TestAreaTextbox.Text;
			Settings.Default.Save();
		}

		private void loadTestContentToolStripMenuItem_Click(object sender, EventArgs e)
		{
			using (UriEditorDialog dlg = new UriEditorDialog())
			{
				dlg.Text = "Select URL for Test Area Content";
				if (dlg.ShowDialog(this.ParentForm) != DialogResult.OK)
				{
					return;
				}

				if (dlg.SelectedUri.IsHttp())
				{
					this.Cursor = Cursors.WaitCursor;
					try
					{
						HttpWebRequest request = (HttpWebRequest)WebRequest.Create(dlg.SelectedUri);
						HttpWebResponse response = (HttpWebResponse)request.GetResponse();

						using (StreamReader reader = new StreamReader(response.GetResponseStream()))
						{
							this.TestAreaTextbox.Text = reader.ReadToEnd();
						}

					}
					finally
					{
						this.Cursor = Cursors.Default;
					}
				}
				else
				{
					MessageBox.Show(this.ParentForm, "Only HTTP URLs are supported for this function. HTTP URLs are URLs that begin with either 'http://' or 'https://'.", "Only HTTP supported", MessageBoxButtons.OK);
				}
			}
		}



		private class Settings : ApplicationSettingsBase
		{

			public static Settings Default = (Settings)ApplicationSettingsBase.Synchronized(new Settings());

			[UserScopedSetting]
			[DefaultSettingValue(null)]
			public string TestAreaContent
			{
				get { return (string)this["TestAreaContent"]; }
				set { this["TestAreaContent"] = value; }
			}
		}

	}


}
